<!doctype html>
<html lang="zh_cn" itemscope itemtype="http://schema.org/Person">
<head>
            <meta charset="utf-8">
        <!-- Site Meta Data -->
        <title>Typecho首页定时生成静态文件-压缩输出-PHP</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="技术博客,涉及Java/PHP/Python/Javascript等,聊聊程序,聊聊生活,聊聊事实,聊聊育儿">
        <meta name="keywords" content="编码经验,技术分享,生活积累,实事评说">
        <meta name="author" content="布丁缘">

        <link rel="shortcut icon" href="">

        <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700' rel='stylesheet' type='text/css'>
        <!-- Style Meta Data -->
        <link rel="stylesheet" href="https://www.ddkiss.com/theme/css/style.css" type="text/css"/>
        <link rel="stylesheet" href="https://www.ddkiss.com/theme/css/pygments.css" type="text/css"/>

        <!-- Feed Meta Data -->
            <link href="https://www.ddkiss.com/feeds/all.atom.xml" type="application/atom+xml" rel="alternate"
                  title="一个程序员的简单生活 ATOM Feed"/>


    <meta name="keywords" content="">
    <link rel="stylesheet" href="//dn-coding-net-public-file.qbox.me/Coding-Comments/v0.1.0/default.css">
</head>

<body>
<!-- Sidebar -->
<aside>
    <center><h1><a href="/" style="color:#fff"><img id="avatar" src="/images/avatar.jpg"></a></h1></center>
        <p>一个程序员的简单生活</p>
    <br>
    <nav class="nav">
        <ul class="list-bare">
                <li><a class="nav__link" href="https://www.ddkiss.com/category/chang-yong-ji-qiao.html">常用技巧</a></li>
                <li><a class="nav__link" href="https://www.ddkiss.com/category/kai-fa-huan-jing.html">开发环境</a></li>
                <li><a class="nav__link" href="https://www.ddkiss.com/category/sheng-huo-dian-di.html">生活点滴</a></li>

                <li><a class="nav__link" href="/pages/books.html">书单</a></li>
                <li><a class="nav__link" href="/pages/movies.html">影单</a></li>
                <li><a class="nav__link" href="/pages/downloads.html">下载</a></li>
                <li><a class="nav__link" href="/pages/about.html">关于我</a></li>


        </ul>
    </nav>


    <form>
      <input type="text"  id="bdcsMain"/>
    </form>

</aside>

<!-- Content -->
<article>
  <main>
    <nav>
      <a href="/">首页</a>
      <a href="/archives.html">归档</a>
      <a href="/categories.html">分类</a>
      <a href="/tags.html">标签</a>
      <a href="/pages/about.html">关于我</a>
    </nav>
  </main>
    <section id="content">
        <article>
            <h2 class="post_title post_detail"><a href="https://www.ddkiss.com/archives/70.html" rel="bookmark"
                                                  title="Permalink to Typecho首页定时生成静态文件-压缩输出-PHP">Typecho首页定时生成静态文件-压缩输出-PHP</a></h2>

            <div class="post_list">
                <span>作者：</span><a href="https://www.ddkiss.com/author/bu-ding-yuan.html">布丁缘</a>
                <span class="post_category">分类：<a href="https://www.ddkiss.com/category/kai-fa-huan-jing.html" rel="bookmark"
                                               title="Permalink to 开发环境">开发环境</a></span>
                <span class="post_date">  时间：2017-10-20 16:30:00</span>

            </div>
            <div class="entry-content blog-post">
                <p>最近做首页优化，主要包含三部分：静态生成、内容压缩、定时刷新。下面介绍这三步：</p>
<h2>静态生成</h2>
<p>在blog根目录下新建一个 staticIndex.php 文件，内容如下：</p>
<div class="highlight"><pre><span></span><span class="cp">&lt;?php</span>

<span class="nb">ob_start</span><span class="p">();</span> <span class="c1">//打开缓冲区</span>
<span class="k">include</span><span class="p">(</span><span class="s2">&quot;index.php&quot;</span><span class="p">);</span>
<span class="nv">$content</span> <span class="o">=</span> <span class="nb">ob_get_contents</span><span class="p">();</span> <span class="c1">//得到缓冲区的内容</span>

<span class="nv">$fp</span> <span class="o">=</span> <span class="nb">fopen</span><span class="p">(</span><span class="s2">&quot;index.html&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">);</span>
<span class="nb">fwrite</span><span class="p">(</span><span class="nv">$fp</span><span class="p">,</span> <span class="nv">$content</span><span class="p">);</span>
<span class="nb">fclose</span><span class="p">(</span><span class="nv">$fp</span><span class="p">);</span>
<span class="cp">?&gt;</span>
</pre></div>


<p>代码也很简单，引入index.php的内容，然后缓存，最后访问页面时输出当前内容。
<strong>注意：</strong>index.html文件的读写权限一定要有，不然会报错。</p>
<h2>内容压缩</h2>
<p>过滤字符，比如下面的函数</p>
<div class="highlight"><pre><span></span><span class="kd">function</span> <span class="nx">compress</span><span class="p">(</span><span class="nx">$string</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="nx">ltrim</span><span class="p">(</span><span class="nx">rtrim</span><span class="p">(</span><span class="nx">preg_replace</span><span class="p">(</span><span class="nx">array</span><span class="p">(</span><span class="s2">&quot;/&gt; *([^ ]*) *&lt;/&quot;</span><span class="p">,</span><span class="s2">&quot;//&quot;</span><span class="p">,</span><span class="s2">&quot;&#39;/\*[^*]*\*/&#39;&quot;</span><span class="p">,</span><span class="s2">&quot;/\r\n/&quot;</span><span class="p">,</span><span class="s2">&quot;/\n/&quot;</span><span class="p">,</span><span class="s2">&quot;/\t/&quot;</span><span class="p">,</span><span class="s1">&#39;/&gt;[ ]+&lt;/&#39;</span><span class="p">),</span>
         <span class="nx">array</span><span class="p">(</span><span class="s2">&quot;&gt;\\1&lt;&quot;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">,</span><span class="s1">&#39;&gt;&lt;&#39;</span><span class="p">),</span><span class="nx">$string</span><span class="p">)));</span>
<span class="p">}</span>
</pre></div>


<p>然后页面输出时调用即可。</p>
<h2>定时生成</h2>
<p>使用crontab来定时curl ****/staticIndex.php，定时访问某个地址。代码如下</p>
<div class="highlight"><pre><span></span>crontab -e

输入如下内容后保存退出
0 0 10,14,16 * * ?   /usr/bin/curl https://www.ddkiss.com/staticIndex.php

service crond restart
</pre></div>
            </div>
            <div class="post_list">
              <div><span>Tags : </span>
                          <span><a href="https://www.ddkiss.com/tag/typeho.html">#typeho, </a></span>
              </div>
            </div>
        </article>
        <div id="container"></div>
        <script type="text/javascript" src="//dn-coding-net-public-file.qbox.me/Coding-Comments/v0.1.0/gitment.min.js"></script>
        <script>
            var gitment = new Gitment({
              owner: 'whusl',
              repo: 'BlogComments',
              oauth: {
                client_id: '621866266817529fba46681653017809',
                client_secret: '14188411740b12ae52159cee9b586bf85cd54125',
              },
            })
            document.getElementById('container').appendChild(gitment.render())
          </script>
    </section>
</article>

<!-- Footer -->
    <footer>
        <p> &copy;2017-2020&nbsp;<a href="http://www.miitbeian.gov.cn/" target="_blank">鄂ICP备17020200号</a>
          Blog powered by <a href="http://getpelican.com/">Pelican</a>
        </p>
    </footer>

    <!-- Analytics -->
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?88c55edaf311dbacac56a16316b04c8b";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>

<script type="text/javascript">(function(){document.write(unescape('%3Cdiv id="bdcs"%3E%3C/div%3E'));var bdcs = document.createElement('script');bdcs.type = 'text/javascript';bdcs.async = true;bdcs.src = 'http://znsv.baidu.com/customer_search/api/js?sid=14490611060029767912' + '&plate_url=' + encodeURIComponent(window.location.href) + '&t=' + Math.ceil(new Date()/3600000);var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(bdcs, s);})();</script>

</body>
</html>